﻿Public Class clsJPck
    Public Function insert(ck_in_code As String, oper_type As String, material_code As String, into_num As String, location As String, oper As String, validity As String) As Integer
        Dim sql As String = ""
        Dim rtn As Integer
        DBacs.BeginTransaction()
        Try
            sql &= "INSERT INTO sf.b_jp_ck_in_details(ck_in_code, oper_type, material_code, into_num, location, operator, update_time, validity) "
            sql &= "VALUES ( ?, ?, ?, ?, ?, ?, (SELECT CURRENT_TIMESTAMP(6)), ?)"
            rtn = DBacs.ExecuteUpdateSql(sql, -1, ck_in_code, oper_type, material_code, into_num, location, oper, validity)
            sql = ""
            sql &= " select * from sf.b_jp_ck where oper_type = ? and material_code = ? "
            Dim dt As DataTable = DBacs.ExecuteSql(sql, -1, oper_type, material_code)
            sql = ""
            If dt.Rows.Count = 0 Then
                sql &= "INSERT INTO sf.b_jp_ck (oper_type, material_code, cur_num, update_time) "
                sql &= "VALUES ( ?, ?, ?, (SELECT CURRENT_TIMESTAMP(6)))"
                rtn = DBacs.ExecuteUpdateSql(sql, -1, oper_type, material_code, into_num)
            ElseIf dt.Rows.Count = 1 Then
                sql &= "UPDATE sf.b_jp_ck  "
                sql &= " SET cur_num = cur_num + ?, update_time = (SELECT CURRENT_TIMESTAMP(6)) "
                sql &= " WHERE oper_type = ? and material_code = ? "
                rtn = DBacs.ExecuteUpdateSql(sql, -1, into_num, oper_type, material_code)
            End If
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        End Try
        Return rtn
    End Function

End Class
